<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class User extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $sid = input('shop_id');
        if($sid){
            model('user')->where(['sid'=> $sid]);
        }
        $list = model('user')->order('id desc')->paginate(config('page_number'));
        return $this->fetch('index', ['list' => $list]);
    }

    public function add()
    {
        $sid = input('shop_id');
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['sid'] = $sid;
            empty($param['password']) && $this->error('密码不能为空');
            if ($this->insert('user', $param) === true) {
                model('userAuthGroupAccess')->save(['uid' => $this->insertId, 'group_id' => $param['group_id']]);
                insert_admin_log('添加了用户');
                $this->success('添加成功', url('admin/user/shop'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', ['userAuthGroup' => model('userAuthGroup')->where(['sid'=>$sid,'status'=>1])->select()]);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true);
            if (empty($param['password'])) {
                unset($param['password']);
            }
            if ($this->update('user', $param, input('_verify', true)) === true) {
                $verify && model('userAuthGroupAccess')->save(['group_id' => $param['group_id']], ['uid' => $param['id']]);
                insert_admin_log('修改了用户');
                $this->success('修改成功', url('admin/user/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
            'data' => model('user')->with('userAuthGroupAccess')->where('id', input('id'))->find(),
            'userAuthGroup' => model('userAuthGroup')->where(['sid'=>$sid,'status'=>1])->select(),]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if ($this->delete('user', $this->request->param()) === true) {
                model('userAuthGroupAccess')->where('uid', $param['id'])->delete();
                insert_admin_log('删除了用户');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }

    public function export()
    {
        $data = collection(model('user')->field('id,username,mobile')->order('id desc')->select())->toArray();
        array_unshift($data, ['ID', '用户名', '手机号']);
        insert_admin_log('导出了用户');
        export_excel($data, date('YmdHis'));
    }

    public function log()
    {
        return $this->fetch('log', ['list' => model('userLog')->order('create_time desc')->paginate(config('page_number'))]);
    }

    public function truncate()
    {
        if ($this->request->isPost()) {
            db()->query('TRUNCATE ' . config('database.prefix') . 'user_log');
            $this->success('操作成功');
        }
    }
    //学校列表
    public function shop()
    {
        return $this->fetch('shop', ['list' => model('shop')->order('id desc')->paginate(config('page_number'))]);
    }
    //学校添加
    public function shop_add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['create_time'] = time();
            $verify = input('_verify', true);
            if ($this->insert('shop', $param) === true) {
                insert_admin_log('添加了学校');
                $this->success('添加成功', url('admin/user/shop'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('shop_save');
    }
    //学校编辑
    public function shop_edit()
    {
        $id = input('id') ?: '参数错误';
        $data = model('shop')->get($id);
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['update_time'] = time();
            $verify = input('_verify', true);
            if ($this->update('shop', $param, $verify) === true) {
                insert_admin_log('修改了商家');
                $this->success('修改成功', url('admin/user/shop'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('shop_save',[
            'data' => $data,
        ]);
    }
    //学校删除
    public function shop_del()
    {
        if ($this->request->isPost()) {
            if ($this->delete_recovery('shop', $this->request->param()) === true) {
                insert_admin_log('删除了商家');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }
    
    
}
